Hệ thống quản lý phương tiện trong PHP
1 <?php
2 $target_dir = "uploads/";
3 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
4 $uploadOk = 1;
5 $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
6 // Check if image file is a actual image or fake image
7 if(isset($_POST["submit"])) {
8 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
9 if($check !== false) {
10 echo "File is an image - " . $check["mime"] . ".";
11 $uploadOk = 1;
12 } else {
13 echo "File is not an image.";
14 $uploadOk = 0;
15 }
16 }
17 // Check if file already exists
18 if (file_exists($target_file)) {
19 echo "Sorry, file already exists.";
20 $uploadOk = 0;
21 }
22 // Check file size
23 if ($_FILES["fileToUpload"]["size"] > 500000) {
24 echo "Sorry, your file is too large.";
25 $uploadOk = 0;
26 }
27 // Allow certain file formats
28 if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
29 && $imageFileType != "gif" ) {
30 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
31 $uploadOk = 0;
32 }
33 // Check if $uploadOk is set to 0 by an error
34 if ($uploadOk == 0) {
35 echo "Sorry, your file was not uploaded.";
36 // if everything is ok, try to upload file
37 } else {
38 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
39 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
40 } else {
41 echo "Sorry, there was an error uploading your file.";
42 }
43 }
44 ?>